from __future__ import annotations
from litestar import Litestar
from litestar.repository import FilterTypes
from litestar.contrib.sqlalchemy.plugins import AsyncSessionConfig, SQLAlchemyAsyncConfig, SQLAlchemyInitPlugin
from controllers.test1 import Test1Controller
from models.test1 import Test1Model
from lib.dependencies import create_collection_dependencies

session_config = AsyncSessionConfig(expire_on_commit=False)
sqlalchemy_config = SQLAlchemyAsyncConfig(
    connection_string="sqlite+aiosqlite:///test.sqlite", session_config=session_config
) 
sqlalchemy_plugin = SQLAlchemyInitPlugin(config=sqlalchemy_config)


async def on_startup() -> None:
    """Initializes the database."""
    async with sqlalchemy_config.get_engine().begin() as conn:
        await conn.run_sync(Test1Model.metadata.create_all)

dependencies = create_collection_dependencies()

app = Litestar(
    route_handlers=[Test1Controller],
    on_startup=[on_startup],
    plugins=[SQLAlchemyInitPlugin(config=sqlalchemy_config)],
    dependencies=dependencies,
    signature_namespace={
        "FilterTypes": FilterTypes,
    }
)